
import QtQuick 1.0

import CustomItems 1.0
import SocketItems 1.0

Rectangle {
    id: main
    width: 360; height: 640
    color: "white"

    Image {
        id: backround
        source: "electricity.png"
        fillMode: Image.PreserveAspectFit
        anchors.centerIn: parent
    }

    CameraItem {
        id: camera
        anchors.centerIn: parent
        width: parent.width * 0.8
        height: parent.width * 0.8
        clip: true

        onKwhWaste: {
            kwhText.text = value + " kWh";
            serverSocket.sendToClient(value);
        }

        onLedOff: {
        }

        onLedOn: {
            if (!ledTimer.running) {
                led.opacity = 1;
                ledTimer.restart();
            }
        }
    }

    Text {
        id: kwhText
        font.pixelSize: 40
        color: "black"
        font.bold: true
        clip: true
        smooth: true
        anchors.top: camera.bottom
        anchors.horizontalCenter: camera.horizontalCenter
    }

    ServerSocket {
        id: serverSocket

        onServerLog: {
            console.log(log);
        }
    }


    Timer {
        id: ledTimer
        interval: 200
        onTriggered: {
            led.opacity = 0;
        }
    }

    Timer {
        id: hide
        interval: 30000
        onTriggered: {
            camera.hideViewFinder();
            camera.opacity = 0;
        }
    }

    Rectangle {
        id: led
        anchors.bottom: camera.top
        anchors.horizontalCenter: camera.horizontalCenter
        anchors.bottomMargin: 20
        //anchors.centerIn: camera
        clip: true
        smooth: true
        color: "red"
        width: 20
        height: 20
        radius: 4
        opacity: 0
    }

    Component.onCompleted: {
        serverSocket.start();
        camera.enableCamera(true);
        hide.restart();
    }

    MouseArea {
        anchors.fill: parent;
        onClicked: {
            camera.showViewFinder();
            camera.opacity = 1;
        }
    }
}
